package cn.lzd.mr.case4;

import cn.lzd.mr.PathUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by lzd on 2018/6/1.
 * 分组topn的简单实现
 */
public class Case4MR {
    static class Mp extends Mapper<LongWritable, Text, Text, Case4Bean> {

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] split = value.toString().split(",");
            Case4Bean bean = new Case4Bean(split[0], split[1], split[2], Float.parseFloat(split[3]), Integer.parseInt(split[4]));
            context.write(new Text(split[0]), bean);
        }
    }

    static class Rd extends Reducer<Text, Case4Bean, NullWritable, Case4Bean> {

        @Override
        protected void reduce(Text key, Iterable<Case4Bean> values, Context context) throws IOException, InterruptedException {
            int count = context.getConfiguration().getInt("top.n", 1);
            int i = 0;

            List<Case4Bean> list = new ArrayList<>();

            for (Case4Bean value : values) {
                try {
                    Case4Bean bean = new Case4Bean();
                    BeanUtils.copyProperties(bean, value);
                    list.add(bean);
                    if ((++i) == count) {
                        break;
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }

            Collections.sort(list);

            list.forEach((x) -> {
                System.out.println(x);
                try {
                    context.write(NullWritable.get(), x);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration configuration = new Configuration();
        configuration.setInt("top.n", 3);
        Job job = Job.getInstance(configuration);
        job.setJarByClass(Case4MR.class);

        job.setOutputKeyClass(Case4Bean.class);
        job.setOutputValueClass(NullWritable.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Case4Bean.class);

        job.setMapperClass(Mp.class);
        job.setReducerClass(Rd.class);

        FileInputFormat.setInputPaths(job, new Path(PathUtil.getpath("case4") + "text"));
        FileOutputFormat.setOutputPath(job, new Path(PathUtil.getpath("case4") + "out/"));

        System.exit(job.waitForCompletion(true) ? 0 : 1);

    }
}